<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>Android自定义View-03 属性动画 | 码行</title><meta name="author" content="Carl Su"><meta name="copyright" content="Carl Su"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="属性动画视图动画仅能对指定的控件做动画，而属性动画是通过改变空间的某一属性值来做动画的 补间动画虽然能够对控件做动画，但是并没有改变控件内部的属性值。 3.1简单实现ValueAnimator valueAnimator &#x3D; ValueAnimator.ofInt(0,400);valueAnimator.setDuration(1000L);&#x2F;&#x2F;添加监听事件valueAnimator.addUp">
<meta property="og:type" content="article">
<meta property="og:title" content="Android自定义View-03 属性动画">
<meta property="og:url" content="https://devsu.top/posts/f8577dac/index.html">
<meta property="og:site_name" content="码行">
<meta property="og:description" content="属性动画视图动画仅能对指定的控件做动画，而属性动画是通过改变空间的某一属性值来做动画的 补间动画虽然能够对控件做动画，但是并没有改变控件内部的属性值。 3.1简单实现ValueAnimator valueAnimator &#x3D; ValueAnimator.ofInt(0,400);valueAnimator.setDuration(1000L);&#x2F;&#x2F;添加监听事件valueAnimator.addUp">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s2.loli.net/2023/03/14/4qo6phTg3CRv71H.png">
<meta property="article:published_time" content="2020-08-05T03:59:14.000Z">
<meta property="article:modified_time" content="2023-03-15T07:07:33.713Z">
<meta property="article:author" content="Carl Su">
<meta property="article:tag" content="Android">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2023/03/14/4qo6phTg3CRv71H.png"><link rel="shortcut icon" href="https://s2.loli.net/2023/03/09/whMHdYkrKoIBnSC.png"><link rel="canonical" href="https://devsu.top/posts/f8577dac/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?6243425e2ecb037931274c4ca1db3132";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":300},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: Carl Su","link":"链接: ","source":"来源: 码行","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Android自定义View-03 属性动画',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-03-15 15:07:33'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const now = new Date()
          const hour = now.getHours()
          const isNight = hour <= 6 || hour >= 18
          if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
          else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>const preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> { preloader.endLoading() })

if (false) {
  document.addEventListener('pjax:send', () => { preloader.initLoading() })
  document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://s2.loli.net/2023/03/09/QrI9mgJ4XlbEPzN.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">169</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">37</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">38</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg fixed" id="page-header" style="background-image: url('https://s2.loli.net/2023/03/14/4qo6phTg3CRv71H.png')"><nav id="nav"><span id="blog-info"><a href="/" title="码行"><img class="site-icon" src="https://s2.loli.net/2023/03/09/whMHdYkrKoIBnSC.png"/><span class="site-name">码行</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Android自定义View-03 属性动画</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-08-05T03:59:14.000Z" title="发表于 2020-08-05 11:59:14">2020-08-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-03-15T07:07:33.713Z" title="更新于 2023-03-15 15:07:33">2023-03-15</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Android/">Android</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Android/%E8%87%AA%E5%AE%9A%E4%B9%89View/">自定义View</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.9k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>8分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Android自定义View-03 属性动画"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="属性动画"><a href="#属性动画" class="headerlink" title="属性动画"></a>属性动画</h2><p><em><strong>视图动画仅能对指定的控件做动画，而属性动画是通过改变空间的某一属性值来做动画的</strong></em></p>
<p>补间动画虽然能够对控件做动画，但是并没有改变控件内部的属性值。</p>
<h3 id="3-1简单实现"><a href="#3-1简单实现" class="headerlink" title="3.1简单实现"></a>3.1简单实现</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">ValueAnimator</span> <span class="variable">valueAnimator</span> <span class="operator">=</span> ValueAnimator.ofInt(<span class="number">0</span>,<span class="number">400</span>);</span><br><span class="line">valueAnimator.setDuration(<span class="number">1000L</span>);</span><br><span class="line"><span class="comment">//添加监听事件</span></span><br><span class="line">valueAnimator.addUpdateListener(<span class="keyword">new</span> <span class="title class_">ValueAnimator</span>.AnimatorUpdateListener() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onAnimationUpdate</span><span class="params">(ValueAnimator animator)</span> &#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">curValue</span> <span class="operator">=</span> (<span class="type">int</span>) animator.getAnimatedValue();</span><br><span class="line">      <span class="comment">//通过更改控件left、top、right、bottom这四个点的坐标来更改坐标位置</span></span><br><span class="line">      <span class="comment">//textView将从屏幕左上角点(0，0)运行到点(400，400)</span></span><br><span class="line">        textView.layout(curValue,curValue,curValue + textView.getWidth(), curValue + textView.getHeight());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">//设置重复次数，ValueAnimator.INFINITE 表示无限循环</span></span><br><span class="line">valueAnimator.setRepeatCount(ValueAnimator.INFINITE);</span><br><span class="line"><span class="comment">//设置重复模式 RESTART 和 REVERSE</span></span><br><span class="line">valueAnimator.setRepeatMode(ValueAnimator.RESTART);</span><br><span class="line"><span class="comment">//开始动画</span></span><br><span class="line">valueAnimator.start();</span><br></pre></td></tr></table></figure>

<h4 id="3-1-1-ofInt和ofFloat"><a href="#3-1-1-ofInt和ofFloat" class="headerlink" title="3.1.1 ofInt和ofFloat"></a>3.1.1 ofInt和ofFloat</h4><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> ValueAnimator <span class="title function_">ofInt</span><span class="params">(<span class="type">int</span>... values)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> ValueAnimator <span class="title function_">ofFloat</span><span class="params">(<span class="type">float</span>... values)</span></span><br></pre></td></tr></table></figure>

<p>接受类型都是可变长参数，所以我们可以传入任何数量的值；传进去的值列表就表示动画时的变化范围，如ofInt(0,400,200)就表示从数字0变化到400再变化到数字200，我们传进去的数字越多，动画变化就越复杂。ofInt和ofFloat唯一的区别就是参数类型不一样。</p>
<h4 id="3-1-2-常用函数"><a href="#3-1-2-常用函数" class="headerlink" title="3.1.2 常用函数"></a>3.1.2 常用函数</h4><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">Object <span class="title function_">getAnimatedValue</span><span class="params">()</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">cancel</span><span class="params">()</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">pause</span><span class="params">()</span> <span class="comment">//暂停</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">resume</span><span class="params">()</span> <span class="comment">//恢复</span></span><br></pre></td></tr></table></figure>

<p><em><strong>注意:重复次数为INFINITE（无限循环）的动画，当Activity结束的时候，必须调用cancel()函数取消动画，否则动画将无限循环，从而导致View无法释放，进一步导致整个Activity无法释放，引起内存泄漏</strong></em></p>
<h4 id="3-1-3-添加与移除监听器"><a href="#3-1-3-添加与移除监听器" class="headerlink" title="3.1.3 添加与移除监听器"></a>3.1.3 添加与移除监听器</h4><p>1.添加监听器</p>
<p>在前面已经添加了一个监听器 addUpdateListener，以监听动画过程中值的实时变化，在ValueAnimator中共有三个监听器。</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//监听器一：监听动画过程中值的变化</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">addUpdateListener</span><span class="params">(ValueAnimator.AnimatorUpdateListener listener)</span></span><br><span class="line"><span class="comment">//监听动画变化时的4个状态</span></span><br><span class="line">valueAnimator.addListener(<span class="keyword">new</span> <span class="title class_">Animator</span>.AnimatorListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onAnimationStart</span><span class="params">(Animator animator)</span> &#123;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onAnimationEnd</span><span class="params">(Animator animator)</span> &#123;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onAnimationCancel</span><span class="params">(Animator animator)</span> &#123;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onAnimationRepeat</span><span class="params">(Animator animator)</span> &#123;</span><br><span class="line">            &#125;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">//监听动画暂停/恢复 的状态</span></span><br><span class="line">valueAnimator.addPauseListener(<span class="keyword">new</span> <span class="title class_">Animator</span>.AnimatorPauseListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onAnimationPause</span><span class="params">(Animator animator)</span> &#123;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onAnimationResume</span><span class="params">(Animator animator)</span> &#123;</span><br><span class="line">            &#125;</span><br><span class="line"> &#125;);</span><br></pre></td></tr></table></figure>

<p>2.移除监听器</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">removePauseListener</span><span class="params">(Animator.AnimatorPauseListener listener)</span> </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">removeUpdateListener</span><span class="params">(ValueAnimator.AnimatorUpdateListener listener)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">removeUpdateListener</span><span class="params">(ValueAnimator.AnimatorUpdateListener listener)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">removeAllListeners</span><span class="params">()</span></span><br></pre></td></tr></table></figure>

<p>这部分比较简单，就不详细描述了。</p>
<h3 id="3-2-示例：弹跳加载中效果"><a href="#3-2-示例：弹跳加载中效果" class="headerlink" title="3.2 示例：弹跳加载中效果"></a>3.2 示例：弹跳加载中效果</h3><h3 id="3-3-自定义插值器与Evaluator"><a href="#3-3-自定义插值器与Evaluator" class="headerlink" title="3.3 自定义插值器与Evaluator"></a>3.3 自定义插值器与Evaluator</h3><h4 id="3-3-2-示例：抛物动画"><a href="#3-3-2-示例：抛物动画" class="headerlink" title="3.3.2 示例：抛物动画"></a>3.3.2 示例：抛物动画</h4><h3 id="3-4-ObjectAnimator"><a href="#3-4-ObjectAnimator" class="headerlink" title="3.4 ObjectAnimator"></a>3.4 ObjectAnimator</h3><h4 id="3-4-1-简单示例"><a href="#3-4-1-简单示例" class="headerlink" title="3.4.1 简单示例"></a>3.4.1 简单示例</h4><p>下例代码实现了将TextView透明度从0到1的变化过程。</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">ObjectAnimator</span> <span class="variable">objectAnimator</span> <span class="operator">=</span> ObjectAnimator.ofFloat(textView,<span class="string">&quot;alpha&quot;</span>,<span class="number">1.0f</span>,<span class="number">0.0f</span>,<span class="number">1.0f</span>);</span><br><span class="line">objectAnimator.setDuration(<span class="number">2000L</span>);</span><br><span class="line">objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> ObjectAnimator <span class="title function_">ofFloat</span><span class="params">(Object target, String propertyName, <span class="type">float</span>... values)</span></span><br></pre></td></tr></table></figure>

<ul>
<li>第一个参数用于指定这个动画要操作的是哪个控件</li>
<li>第二个参数用于指定这个动画要操作这个控件的哪个属性。</li>
<li>第三个参数时可变长按书，是指这个属性值如何变化。</li>
</ul>
<p>我们来看看如何实现旋转效果.</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">ObjectAnimator</span> <span class="variable">rotateAnimator</span> <span class="operator">=</span> ObjectAnimator.ofFloat(button,<span class="string">&quot;rotation&quot;</span>,<span class="number">0</span>,<span class="number">270</span>,<span class="number">0</span>);</span><br></pre></td></tr></table></figure>

<h4 id="3-4-2-set函数"><a href="#3-4-2-set函数" class="headerlink" title="3.4.2 set函数"></a>3.4.2 set函数</h4><p>我们通过查看<code>TextView</code>的源代码可以发现，它和它的父类<code>View</code>都没有<code>rotation</code>这个属性，那么它是怎么改变这个值的呢？其实<code>ObjectAnimator</code>做动画，并不是根据控件XML的属性来改变的，而是通过指定属性对应的<code>set</code>函数来改变的。比如上面的<code>rotation</code>属性值，<code>ObjectAnimator</code>在动画时就会到指定控件中去找对应的<code>setRotation()</code>函数来改变控件中对应的值。在View中，有关动画共有下面几组set函数。</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//1.透明度 alpha</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAlpha</span><span class="params">(<span class="type">float</span> alpha)</span></span><br><span class="line"><span class="comment">//旋转度数: rotation、rotationX、rotationY</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setRotation</span><span class="params">(<span class="type">float</span> rotation)</span>  <span class="comment">//围绕Z轴旋转</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setRotationY</span><span class="params">(<span class="type">float</span> rotationY)</span>	<span class="comment">//围绕Y轴旋转</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setRotationX</span><span class="params">(<span class="type">float</span> rotationX)</span>	<span class="comment">//围绕X轴旋转</span></span><br><span class="line"><span class="comment">//平移： translationX、translationY</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setTranslationX</span><span class="params">(<span class="type">float</span> translationX)</span> <span class="comment">// X轴平移</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setTranslationY</span><span class="params">(<span class="type">float</span> translationY)</span>	<span class="comment">//Y轴平移</span></span><br><span class="line"><span class="comment">//缩放： scaleX、scaleY</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setScaleX</span><span class="params">(<span class="type">float</span> scaleX)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setScaleY</span><span class="params">(<span class="type">float</span> scaleY)</span></span><br></pre></td></tr></table></figure>



<h4 id="3-4-3-自定义ObjectAnimator属性"><a href="#3-4-3-自定义ObjectAnimator属性" class="headerlink" title="3.4.3  自定义ObjectAnimator属性"></a>3.4.3  自定义ObjectAnimator属性</h4><p>同样实现小球抛物线效果</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> ObjectAnimator <span class="title function_">ofObject</span><span class="params">(Object target, String propertyName, TypeEvaluator evaluator, Object... values)</span> </span><br></pre></td></tr></table></figure>

<p>观察 ObjectAnimator的ofObject生成方法，需要实现自定义的TypeEvaluator，以及自定义propertyName属性</p>
<p>1.自定义ImageView</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FallingBallImageView</span> <span class="keyword">extends</span> <span class="title class_">androidx</span>.appcompat.widget.AppCompatImageView &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">FallingBallImageView</span><span class="params">(Context context, <span class="meta">@Nullable</span> AttributeSet attrs)</span> &#123;</span><br><span class="line">        <span class="built_in">super</span>(context, attrs);</span><br><span class="line">    &#125;</span><br><span class="line">  	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setFallingPos</span><span class="params">(Point point)</span>&#123;</span><br><span class="line">        layout(point.x,point.y , getWidth() + point.x, + getHeight() + point.y);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这段代码中，只有一个<code>set</code>函数。注意点</p>
<ul>
<li>这个set函数对应的属性应该是<code>fallingPos</code>或者<code>FallingPos</code></li>
<li>在<code>setFallingPos()</code>函数中，参数类型时Point对象，所以我们在狗仔ObjectAnimator时，必须使用ofObject()函数。</li>
</ul>
<p>2:布局代码：</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;utf-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">LinearLayout</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:app</span>=<span class="string">&quot;http://schemas.android.com/apk/res-auto&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:tools</span>=<span class="string">&quot;http://schemas.android.com/tools&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">tools:context</span>=<span class="string">&quot;.MainActivity&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">com.carl.demo.customobjectanimator.FallingBallImageView</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">&quot;@+id/image&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">&quot;50dp&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">&quot;50dp&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:src</span>=<span class="string">&quot;@drawable/shape_circle&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">app:layout_constraintLeft_toLeftOf</span>=<span class="string">&quot;parent&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">app:layout_constraintTop_toTopOf</span>=<span class="string">&quot;parent&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">Button</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">&quot;@+id/button&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_marginStart</span>=<span class="string">&quot;40dp&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">&quot;wrap_content&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">&quot;wrap_content&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:text</span>=<span class="string">&quot;Button&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>shape_circle.xml</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="line">&lt;shape xmlns:android=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="line">    android:shape=<span class="string">&quot;oval&quot;</span>&gt;</span><br><span class="line">    &lt;solid android:color=<span class="string">&quot;#FF4949&quot;</span>/&gt;</span><br><span class="line">&lt;/shape&gt;</span><br></pre></td></tr></table></figure>

<p>3: 自定义TypeEvaluator</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FallingBallEvaluator</span> <span class="keyword">implements</span> <span class="title class_">TypeEvaluator</span>&lt;Point&gt; &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="type">Point</span> <span class="variable">point</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Point</span>();</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> Point <span class="title function_">evaluate</span><span class="params">(<span class="type">float</span> fraction, Point startValue, Point endValue)</span> &#123;</span><br><span class="line">        point.x = (<span class="type">int</span>) (startValue.x + fraction *(endValue.x - startValue.x));</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (fraction * <span class="number">2</span> &lt;= <span class="number">1</span>)&#123;</span><br><span class="line">            point.y = (<span class="type">int</span>)(startValue.y + fraction * <span class="number">2</span>*(endValue.y - startValue.y));</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            point.y = endValue.y;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> point;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>4.使用</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">ObjectAnimator</span> <span class="variable">objectAnimator</span> <span class="operator">=</span> ObjectAnimator.ofObject(</span><br><span class="line">        ball,<span class="string">&quot;fallingPos&quot;</span>,</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">FallingBallEvaluator</span>(),</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">Point</span>(<span class="number">0</span>,<span class="number">0</span>),</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">Point</span>(<span class="number">500</span>,<span class="number">500</span>));</span><br><span class="line">objectAnimator.setDuration(<span class="number">2000L</span>);</span><br><span class="line">objectAnimator.start();</span><br></pre></td></tr></table></figure>

<p>整体过程时：当点击按钮时开始动画， ObjectAnimator.ofObject() 函数会根据 FallingBallEvaluator 实时得到当前的 Point 值；然后到 ball_imageView 控件中去找 setFallingPos(Point point) 函数，它的参数就是 FallingBallEvaluator 返回的 Point 对象 ；在找到 setFallingPos(Point point) 函数后，通过反射调用他。在 setFallingPos(Point point)函数中，我们会根据参数值实时改变圆形的位置。</p>
<h3 id="3-5-组合动画"><a href="#3-5-组合动画" class="headerlink" title="3.5 组合动画"></a>3.5 组合动画</h3><h4 id="3-5-1-playSequentially-与playTogether-函数"><a href="#3-5-1-playSequentially-与playTogether-函数" class="headerlink" title="3.5.1 playSequentially()与playTogether()函数"></a>3.5.1 playSequentially()与playTogether()函数</h4><ul>
<li>playSequentially() 表示动画依次播放</li>
</ul>
<p>函数声明:</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">playSequentially</span><span class="params">(Animator... items)</span> </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">playSequentially</span><span class="params">(List&lt;Animator&gt; items)</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"> <span class="type">ObjectAnimator</span> <span class="variable">colorAnimator</span> <span class="operator">=</span> ObjectAnimator.ofInt(textView1,<span class="string">&quot;BackgroundColor&quot;</span>,</span><br><span class="line">        getColor(R.color.color1),getColor(R.color.color2),getColor(R.color.color3));</span><br><span class="line"><span class="type">ObjectAnimator</span> <span class="variable">translateAnimator</span> <span class="operator">=</span> ObjectAnimator.ofFloat(textView1,<span class="string">&quot;translationY&quot;</span>,</span><br><span class="line">        <span class="number">0</span>,<span class="number">400</span>,<span class="number">0</span>)；</span><br><span class="line"><span class="type">ObjectAnimator</span> <span class="variable">translateAnimator2</span> <span class="operator">=</span> ObjectAnimator.ofFloat(textView2,<span class="string">&quot;translationY&quot;</span>,<span class="number">0</span>,<span class="number">400</span>,<span class="number">0</span>);</span><br><span class="line"><span class="type">AnimatorSet</span> <span class="variable">animatorSet</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AnimatorSet</span>();</span><br><span class="line">animatorSet.setDuration(<span class="number">2000L</span>);</span><br><span class="line">animatorSet.playSequentially(colorAnimator,translateAnimator,translateAnimator2);</span><br><span class="line">animatorSet.start();</span><br></pre></td></tr></table></figure>

<p><img src= "https://s2.loli.net/2023/03/09/dog2qQY83iTIvHM.gif" data-lazy-src="https://raw.githubusercontent.com/winstone-su/imageHosting/main/img/playSequentially_anim.png" alt="布局"></p>
<ul>
<li>playTogether() 表示动画一起播放</li>
</ul>
<p>函数声明:</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">playTogether</span><span class="params">(Animator... items)</span>  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">playTogether</span><span class="params">(Collection&lt;Animator&gt; items)</span></span><br></pre></td></tr></table></figure>

<p>使用:</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">ObjectAnimator</span> <span class="variable">colorAnimator</span> <span class="operator">=</span> ObjectAnimator.ofInt(textView1,<span class="string">&quot;BackgroundColor&quot;</span>,</span><br><span class="line">        getColor(R.color.color1),getColor(R.color.color2),getColor(R.color.color3));</span><br><span class="line"><span class="type">ObjectAnimator</span> <span class="variable">translateAnimator</span> <span class="operator">=</span> ObjectAnimator.ofFloat(textView1,<span class="string">&quot;translationY&quot;</span>,</span><br><span class="line">        <span class="number">0</span>,<span class="number">400</span>,<span class="number">0</span>);</span><br><span class="line">translateAnimator.setStartDelay(<span class="number">2000L</span>);</span><br><span class="line"></span><br><span class="line"><span class="type">ObjectAnimator</span> <span class="variable">translateAnimator2</span> <span class="operator">=</span> ObjectAnimator.ofFloat(textView2,<span class="string">&quot;translationY&quot;</span>,<span class="number">0</span>,<span class="number">400</span>,<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="type">AnimatorSet</span> <span class="variable">animatorSet</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AnimatorSet</span>();</span><br><span class="line">animatorSet.setDuration(<span class="number">2000L</span>);</span><br><span class="line">animatorSet.playTogether(colorAnimator,translateAnimator,translateAnimator2);</span><br><span class="line">animatorSet.start();</span><br></pre></td></tr></table></figure>

<p>上述代码中： 在textView1 和textView2 平移动画上加了一个延时，所以在textView1颜色变化结束后，textview1和textview2会一起移动</p>
<h4 id="3-5-2-如何实现无限循环的组合动画"><a href="#3-5-2-如何实现无限循环的组合动画" class="headerlink" title="3.5.2 如何实现无限循环的组合动画"></a>3.5.2 如何实现无限循环的组合动画</h4><p>是否无限循环主要看动画本身，与playTogether()无关。</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">ObjectAnimator</span> <span class="variable">colorAnimator</span> <span class="operator">=</span> ObjectAnimator.ofInt(textView1,<span class="string">&quot;BackgroundColor&quot;</span>,</span><br><span class="line">                getColor(R.color.color1),getColor(R.color.color2),getColor(R.color.color3));</span><br><span class="line">colorAnimator.setRepeatCount(ValueAnimator.INFINITE);</span><br><span class="line"><span class="type">ObjectAnimator</span> <span class="variable">translateAnimator</span> <span class="operator">=</span> ObjectAnimator.ofFloat(textView1,<span class="string">&quot;translationY&quot;</span>,<span class="number">0</span>,<span class="number">400</span>,<span class="number">0</span>);</span><br><span class="line">translateAnimator.setRepeatCount(ValueAnimator.INFINITE);</span><br><span class="line"><span class="type">ObjectAnimator</span> <span class="variable">translateAnimator2</span> <span class="operator">=</span> ObjectAnimator.ofFloat(textView2,<span class="string">&quot;translationY&quot;</span>,<span class="number">0</span>,<span class="number">400</span>,<span class="number">0</span>);</span><br><span class="line">translateAnimator2.setRepeatCount(ValueAnimator.INFINITE);</span><br><span class="line">...</span><br></pre></td></tr></table></figure>

<h4 id="3-5-3-AnimatorSet-Builder"><a href="#3-5-3-AnimatorSet-Builder" class="headerlink" title="3.5.3 AnimatorSet.Builder"></a>3.5.3 AnimatorSet.Builder</h4></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://devsu.top">Carl Su</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://devsu.top/posts/f8577dac/">https://devsu.top/posts/f8577dac/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://devsu.top" target="_blank">码行</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Android/">Android</a></div><div class="post_share"><div class="social-share" data-image="https://s2.loli.net/2023/03/14/4qo6phTg3CRv71H.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/ddcdc262/" title="Android自定义View-04 属性动画进阶"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Android自定义View-04 属性动画进阶</div></div></a></div><div class="next-post pull-right"><a href="/posts/d2fda48f/" title="Android自定义View-02 视图动画"><img class="cover" src="https://s2.loli.net/2023/03/14/RLM1qPGF3S6sNnJ.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Android自定义View-02 视图动画</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/be1a9334/" title="Android-在Activity中使用Menu"><img class="cover" src="https://img10.51tietu.net/pic/20191030/k2hqdmpfendk2hqdmpfend.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-05</div><div class="title">Android-在Activity中使用Menu</div></div></a></div><div><a href="/posts/59a3d1d6/" title="ConstraintLayout-constraintDimensionRatio属性"><img class="cover" src="https://img0.baidu.com/it/u=2303708579,2686113994&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-05</div><div class="title">ConstraintLayout-constraintDimensionRatio属性</div></div></a></div><div><a href="/posts/f305e41/" title="Android 11 分区存储"><img class="cover" src="https://img9.51tietu.net/pic/20190921/idxelngfttlidxelngfttl.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-05</div><div class="title">Android 11 分区存储</div></div></a></div><div><a href="/posts/b79c82/" title="Android优秀开源第三方库"><img class="cover" src="https://img.qingwk.com/images/resource/global/2020/06/09/96961_46896196822_at.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-05</div><div class="title">Android优秀开源第三方库</div></div></a></div><div><a href="/posts/aae49b39/" title="Hilt基本使用"><img class="cover" src="https://img1.baidu.com/it/u=1939248509,402303850&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-08-05</div><div class="title">Hilt基本使用</div></div></a></div><div><a href="/posts/6ed51752/" title="Android Studio 和Lombok插件不兼容问题"><img class="cover" src="https://img0.baidu.com/it/u=1087455262,471122708&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-05</div><div class="title">Android Studio 和Lombok插件不兼容问题</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div><div id="comment-switch"><span class="first-comment">Valine</span><span class="switch-btn"></span><span class="second-comment">Disqus</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div><div><div id="disqus_thread"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B1%9E%E6%80%A7%E5%8A%A8%E7%94%BB"><span class="toc-number">1.</span> <span class="toc-text">属性动画</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0"><span class="toc-number">1.1.</span> <span class="toc-text">3.1简单实现</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-1-ofInt%E5%92%8CofFloat"><span class="toc-number">1.1.1.</span> <span class="toc-text">3.1.1 ofInt和ofFloat</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-2-%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0"><span class="toc-number">1.1.2.</span> <span class="toc-text">3.1.2 常用函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-3-%E6%B7%BB%E5%8A%A0%E4%B8%8E%E7%A7%BB%E9%99%A4%E7%9B%91%E5%90%AC%E5%99%A8"><span class="toc-number">1.1.3.</span> <span class="toc-text">3.1.3 添加与移除监听器</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-%E7%A4%BA%E4%BE%8B%EF%BC%9A%E5%BC%B9%E8%B7%B3%E5%8A%A0%E8%BD%BD%E4%B8%AD%E6%95%88%E6%9E%9C"><span class="toc-number">1.2.</span> <span class="toc-text">3.2 示例：弹跳加载中效果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8F%92%E5%80%BC%E5%99%A8%E4%B8%8EEvaluator"><span class="toc-number">1.3.</span> <span class="toc-text">3.3 自定义插值器与Evaluator</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-2-%E7%A4%BA%E4%BE%8B%EF%BC%9A%E6%8A%9B%E7%89%A9%E5%8A%A8%E7%94%BB"><span class="toc-number">1.3.1.</span> <span class="toc-text">3.3.2 示例：抛物动画</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-ObjectAnimator"><span class="toc-number">1.4.</span> <span class="toc-text">3.4 ObjectAnimator</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-1-%E7%AE%80%E5%8D%95%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.4.1.</span> <span class="toc-text">3.4.1 简单示例</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-2-set%E5%87%BD%E6%95%B0"><span class="toc-number">1.4.2.</span> <span class="toc-text">3.4.2 set函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-3-%E8%87%AA%E5%AE%9A%E4%B9%89ObjectAnimator%E5%B1%9E%E6%80%A7"><span class="toc-number">1.4.3.</span> <span class="toc-text">3.4.3  自定义ObjectAnimator属性</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-5-%E7%BB%84%E5%90%88%E5%8A%A8%E7%94%BB"><span class="toc-number">1.5.</span> <span class="toc-text">3.5 组合动画</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-5-1-playSequentially-%E4%B8%8EplayTogether-%E5%87%BD%E6%95%B0"><span class="toc-number">1.5.1.</span> <span class="toc-text">3.5.1 playSequentially()与playTogether()函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-5-2-%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0%E6%97%A0%E9%99%90%E5%BE%AA%E7%8E%AF%E7%9A%84%E7%BB%84%E5%90%88%E5%8A%A8%E7%94%BB"><span class="toc-number">1.5.2.</span> <span class="toc-text">3.5.2 如何实现无限循环的组合动画</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-5-3-AnimatorSet-Builder"><span class="toc-number">1.5.3.</span> <span class="toc-text">3.5.3 AnimatorSet.Builder</span></a></li></ol></li></ol></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2018 - 2023 By Carl Su</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">学无止境</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="chat_btn" type="button" title="聊天"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: '',
      appKey: '',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script><script>function loadDisqus () {
  var disqus_config = function () {
    this.page.url = 'https://devsu.top/posts/f8577dac/'
    this.page.identifier = '/posts/f8577dac/'
    this.page.title = 'Android自定义View-03 属性动画'
  };

  window.disqusReset = () => {
    DISQUS.reset({
      reload: true,
      config: disqus_config
    })
  }

  if (window.DISQUS) disqusReset()
  else {
    (function() { 
      var d = document, s = d.createElement('script');
      s.src = 'https://.disqus.com/embed.js';
      s.setAttribute('data-timestamp', +new Date());
      (d.head || d.body).appendChild(s);
    })();
  }

  document.getElementById('darkmode').addEventListener('click', () => {
    setTimeout(() => window.disqusReset(), 200)
  })
}

if ('Valine' === 'Disqus' || !true) {
  if (true) btf.loadComment(document.getElementById('disqus_thread'), loadDisqus)
  else loadDisqus()
} else {
  function loadOtherComment () {
    loadDisqus()
  }
}
</script></div><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = false;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-heart.min.js" async="async" mobile="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></body></html>